package com.deng.maintenance.organization.view
{
	import com.deng.common.vo.OrganizationVo;
	import com.deng.event.ReturnEvent;
	
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.controls.CheckBox;
	import mx.events.FlexEvent;
	import mx.resources.ResourceManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;

	public class OrganizationSelCB extends CheckBox
	{
		private var selVo:OrganizationVo;
		public function OrganizationSelCB()
		{
			this.addEventListener(FlexEvent.CREATION_COMPLETE,showHandler);
			this.addEventListener(MouseEvent.MOUSE_UP,upEvt);
			this.addEventListener(FlexEvent.SHOW,showHandler);
			this.addEventListener(KeyboardEvent.KEY_DOWN,keyHandler);
			super();
////				selVo = this.data as OrganizationVo; 
//				this.addEventListener(FlexEvent.CREATION_COMPLETE,showEvt);	
////				this.addEventListener(MouseEvent.MOUSE_UP,upEvt);			
//				this.addEventListener(FlexEvent.SHOW,showEvt);	
//				
//				super();
			
		}
		private function keyHandler(event:KeyboardEvent):void
		{
			if(event.keyCode == 32)
			{
				event.stopImmediatePropagation();
				return;
			}
		}
		private function upEvt(event:MouseEvent):void{			
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "OrganizationImpl";			
			ro.addEventListener(FaultEvent.FAULT,flt);
			ro.addEventListener(ResultEvent.RESULT,updateRst);
			if(this.selected){//之前选中的，后面要删除
				ro.updateDispenseRelatedId(selVo,0);
				selVo.selected = 0;
//				Alert.show("shanchu");
			}else{//之前没选中，后面要增加
				ro.updateDispenseRelatedId(selVo,1);
				selVo.selected = 1;
//				Alert.show("zengjia");				
			}			
		}
		private function updateRst(evt:ResultEvent):void{
			Alert.show("更新已成功");
			var rtEvt:ReturnEvent=new ReturnEvent(ReturnEvent.loadDispenseManagerList);
			rtEvt.dispatch();
		}
		private function flt(evt:FaultEvent):void{
			Alert.show(evt.fault.getStackTrace());
		}
		private function showHandler(evt:FlexEvent):void{
			selVo = this.data as OrganizationVo;
//			trace("1........"+selVo.dispense_id);
			if(selVo.selected == 1){
				this.selected = true;
			}else{
				this.selected = false;
			}			
		}
		
	}
}